<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head(${user.id != null ? '编辑用户' : '添加用户'})}"></head>
<body>
    <!-- 导航栏 -->
    <nav th:replace="~{fragments/layout :: navbar}"></nav>
    
    <!-- 页面内容 -->
    <div th:replace="~{fragments/layout :: content(~{::main})}">
        <main>
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card shadow">
                        <div class="card-header bg-primary text-white">
                            <h4 class="my-2" th:text="${user.id != null ? '编辑用户' : '添加新用户'}">添加/编辑用户</h4>
                        </div>
                        <div class="card-body p-4">
                            <form th:action="${user.id != null ? '/admin/users/update/' + user.id : '/admin/users/add'}" th:object="${user}" method="post">
                                <input type="hidden" th:if="${user.id != null}" th:field="*{id}">
                                
                                <div class="mb-3">
                                    <label for="username" class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="username" th:field="*{username}" 
                                           th:readonly="${user.id != null}" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></div>
                                    <small class="text-muted" th:if="${user.id != null}">用户名不可修改</small>
                                    <small class="text-muted" th:if="${user.id == null}">用户名将用于登录系统，创建后不可修改</small>
                                </div>
                                
                                <!-- 密码字段，仅在添加用户时显示 -->
                                <div class="mb-3" th:if="${user.id == null}">
                                    <label for="password" class="form-label">密码</label>
                                    <input type="password" class="form-control" id="password" name="password" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></div>
                                    <small class="text-muted">密码长度至少6位</small>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="name" class="form-label">姓名</label>
                                    <input type="text" class="form-control" id="name" th:field="*{name}" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="email" class="form-label">电子邮箱</label>
                                    <input type="email" class="form-control" id="email" th:field="*{email}" required>
                                    <div class="text-danger" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="role" class="form-label">用户角色</label>
                                    <select class="form-select" id="role" name="roleId" required>
                                        <option value="">-- 请选择角色 --</option>
                                        <option value="1" th:selected="${user.isAdmin()}">系统管理员</option>
                                        <option value="2" th:selected="${user.isStudent() || user.id == null}">学生用户</option>
                                    </select>
                                    <div class="text-danger" th:if="${#fields.hasErrors('role')}" th:errors="*{role}"></div>
                                </div>
                                
                                <div class="d-flex justify-content-between mt-4">
                                    <a th:href="@{/admin/users}" class="btn btn-outline-secondary">
                                        <i class="fas fa-arrow-left"></i> 返回
                                    </a>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save"></i> 
                                        <span th:text="${user.id == null ? '添加用户' : '保存更改'}">保存</span>
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
    
    <!-- 页脚 -->
    <footer th:replace="~{fragments/layout :: footer}"></footer>
    
    <!-- 脚本 -->
    <div th:replace="~{fragments/layout :: scripts}"></div>
</body>
</html>
